/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import entity.Account;
import entity.AccountSetting;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Aditya-Z
 */
public class LogikaAccount {

    HttpServletRequest request;

    public LogikaAccount(HttpServletRequest request) {
        this.request = request;
    }

    public String daftar() {
        Account account = new Account();
        AccountSetting ka = new AccountSetting();
        
        String nama_trevel = request.getParameter("Nama travel");
        String alamat = request.getParameter("Alamat");
        String kota = request.getParameter("Kota");
        String kode_pos = request.getParameter("Kode Pos");
        String telepon = request.getParameter("Telepon");
        String email = request.getParameter("Email");
        String pass = request.getParameter("pass");
        String confirm_pass = request.getParameter("Confirm Password");

        account.setNama_trevel(nama_trevel);
        account.setAlamat(alamat);
        account.setKota(kota);
        account.setKode_pos(kode_pos);
        account.setTelepon(telepon);
        account.setEmail(email);
        account.setPassword(pass);
        account.setConfirm_password(confirm_pass);
        // session.setAttribute("tempReg", account);
        
        

        if (nama_trevel == null || alamat == null || kode_pos == null
                || telepon == null || email == null || pass == null 
                || confirm_pass == null) {
            return "errordaftar.jsp";
        } 

            boolean hasilCheck = ka.checkAccount(nama_trevel);
            if (!hasilCheck) {
                ka.createAccount(account);
                return "daftarsukses.jsp";
            } else {
                return "register.jsp";
            }
            
          
       

        }
    }

    public String login() {
        HttpSession session = request.getSession();
        Account account = new Account();
        AccountSetting ka = new AccountSetting();
        String username = request.getParameter("username");
        String password = request.getParameter("pass");

        account = ka.getAccount(username);
        if (username.equals(account.getUsername()) && password.equals(account.getPassword())) {
            session.setMaxInactiveInterval(600);
            session.setAttribute("uname", username);
            request.setAttribute("account", account);
            return "Home.jsp";
        } else {
            return "gagalLogin.jsp";
        }
    }

    public String logout() {
        HttpSession session = request.getSession();
        session.removeAttribute("uname");
        session.invalidate();
        return "formLogin.jsp";
    }

    public String openHome() {
        HttpSession session = request.getSession();
        Account account = new Account();
        AccountSetting ka = new AccountSetting();
        if (session.getAttribute("uname") != null) {
            String username = (String) session.getAttribute("uname");
            account = ka.getAccount(username);
            request.setAttribute("account", account);
            return "Home.jsp";
        } else {
            return "halamanSudahLogout.jsp";
        }

    }
}